package com.pheruagor.handmanager.model.business {
    import flash.errors.IllegalOperationError;

    import mx.collections.ArrayCollection;
    import mx.utils.ObjectUtil;

    /**
     * Club is main entity that contains all data about members (players, manager, etc..)
     * and events (match, training, etc..)
     */
    public class Club {

        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        private var m_id:String;
        private var m_name:String;
        private var m_creationDate:Date;
        private var m_members:ArrayCollection;

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------
        public function Club() {

        }

        //-----------------------------------------------------
        // Access
        //-----------------------------------------------------
        /**
         * Club id
         */
        public function get id():String {
            return m_id;
        }

        /**
         * @private
         */
        public function set id(value:String):void {
            m_id = value;
        }

        /**
         * Club name
         */
        public function get name():String {
            return m_name;
        }

        /**
         * @private
         */
        public function set name(value:String):void {
            m_name = value;
        }

        /**
         * Date of club creation
         */
        public function get creationDate():Date {
            return m_creationDate;
        }

        /**
         * @private
         */
        public function set creationDate(value:Date):void {
            m_creationDate = value;
        }

        /**
         * List of all club members (players, technical staff, etc..)
         */
        [Bindable]
        public function get members():ArrayCollection {
            return m_members
        }

        /**
         * @private
         */
        [ArrayElementType("com.pheruagor.handManager.model.business.Member")]
        public function set members(value:ArrayCollection):void {
            m_members = value;
        }

        //-----------------------------------------------------
        // Operation
        //-----------------------------------------------------
        /**
         * Return a deep copy of the Club with the same id
         */
        public function clone():Club {
            var result:Club = new Club();
            result.id = id;
            result.name = name;
            result.creationDate = new Date(creationDate);

            return result;
        }

        /**
         * Deep comparison with another club
         * TODO : add comparison on members & seasons
         */
        public function equals(club:Club):Boolean {
            if (club == null) {
                return false;
            }

            var idMatch:Boolean = m_id == club.id;
            var nameMatch:Boolean = club.name == name;
            var creationDateMatch:Boolean = ObjectUtil.dateCompare(club.creationDate, creationDate) == 0;
            // TODO : add members comparison
            //var membersMatch:Boolean = membersMatch(club.members);

            if (idMatch && nameMatch && creationDateMatch) {
                return true;
            }

            return false;
        }

        /**
         * Build a Club with an object
         * Used for the conversion between storage and application
         */
        public static function fromObject(item:Object):Club {
            if (!item.hasOwnProperty("id") || !item.hasOwnProperty("name")) {
                throw new IllegalOperationError("Object must contain at least name and id properties");
            }
            var result:Club = new Club();
            result.id = item.id;
            result.name = item.name;
            if (item.hasOwnProperty("creationDate")) {
                result.creationDate = new Date(item.creationDate);
            }

            return result;
        }
        //-----------------------------------------------------
        // Implementation
        //-----------------------------------------------------


    }
}